package UIFree.abox {
	import UIFree.ObjectEvent;
	import UIFree.controls.base.uif_previewSource;
	import UIFree.controls.uif_hscrollArea;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class uif_previewItem extends uif_ecomponent {
		protected var _prevImage	: uif_previewSource;
		protected var _dataItem		: Object;
		
		public function uif_previewItem(index: int, parent:uif_component, ax:int=0, ay:int=0, 
						awidth:int=0, aheight:int=0, a_data: *=null, styleClass:String='', previewURL: String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			scrollArea.items[index].control = this;
			_dataItem 	= a_data;
			_prevImage 	= createImage();
			if (_dataItem) {
				if (scrollArea.selectItem && scrollArea.style['select'] && checkItemSelect(_dataItem)) {
					applyStyle(scrollArea.style['select']);
				};
				
				if (previewURL) {
					_prevImage.loadSource(previewURL + a_data.id + '.jpg');
//					_prevImage.loadSource(uif_util.concatFileName(previewURL, a_data.id + '.jpg'));
				} else if (a_data.preview) _prevImage.loadSource(a_data.preview);
			}
		}
		
		protected function createImage(): uif_previewSource {
			var ir: Rectangle = getInner();
			return new uif_imageSource(this, ir.x, ir.y, ir.width, ir.height, 'preview');
		}
		
		public function select(): void {
			scrollArea.selectIndex = scrollArea.findControl(this);
//				scrollArea.dispatchEvent(new ObjectEvent(Event.SELECT, _dataItem, true, true));
		}
		
		protected function checkItemSelect(item: Object): Boolean {
			return scrollArea.selectItem && scrollArea.selectItem.object && (scrollArea.selectItem.object.id == item.id);
		}
		
		protected function get scrollArea(): uif_hscrollArea {
			return parent.parent as uif_hscrollArea;
		}
		
		public function get data(): * {
			return _dataItem;
		}
	}
}